package com.zzh.interview.controller;

import com.zzh.interview.common.BaseResponse;
import com.zzh.interview.common.ResultUtils;
import com.zzh.interview.config.PhoneMsgConfig;
import com.zzh.interview.service.PhoneService;
import com.zzh.interview.utils.PhoneRightUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 张 志 豪
 * @version 1.0
 */
@RestController
@RequestMapping("/phone")
//@CrossOrigin(origins  = "*", allowCredentials = "true")
public class PhoneController {

    @Autowired
    private PhoneService phoneService;

    //请求手机号发送验证码
    @GetMapping("/sendCode")
    public BaseResponse sendPhoneCode(@RequestParam String toPhone){
        if (StringUtils.isBlank(toPhone)){
            return ResultUtils.error(40001,"手机号不能为空");
        }

        //判断邮箱是否正确
        boolean rightPhone = PhoneRightUtils.isRightPhone(toPhone);
        if (!rightPhone){
            ResultUtils.error(40001,"手机号不正确不合规");
        }

        boolean isSuccess = phoneService.sendCode(toPhone);
        if (isSuccess){
            return ResultUtils.success("验证码已发送至手机号"+toPhone);
        }else {
            return ResultUtils.error(40001,"手机号不正确不合规or欠费");
        }
    }

    @Autowired
    private PhoneMsgConfig phoneMsgConfig;

    //获取信息
    @GetMapping("/getMsg")
    public BaseResponse<String> getMsg(){
        String secretId = phoneMsgConfig.getSecretId();
        String templateId = phoneMsgConfig.getTemplateId();
        String sdkAppId = phoneMsgConfig.getSdkAppId();
        String secretKey = phoneMsgConfig.getSecretKey();
        return ResultUtils.success(secretId+templateId+sdkAppId+secretKey);
    }

}
